package com.lmx.healthcode.ui.activity;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.lmx.healthcode.R;
import com.lmx.healthcode.base.MyApp;
import com.lmx.healthcode.util.Const;
import com.lmx.healthcode.common.base.BaseActivity;
import com.lmx.healthcode.model.bean.Student;
import com.lmx.healthcode.ui.adapter.StudentAdapter;
import com.lmx.healthcode.util.CommonUtil;
import com.lmx.healthcode.util.SharedPreUtil;
import com.lmx.healthcode.util.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * @author Leung
 * @date 2020/9/9 20:27
 */
public class OutSchoolActivity extends BaseActivity {

    @BindView(R.id.img_menu)
    ImageView imgMenu;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    private StudentAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_out_school;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        initRecyclerView();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        if (!MyApp.isFirstCommit()) {
            getData();
        }
    }

    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new StudentAdapter(this, null);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

    }

    private void getData() {
        String studentId = SharedPreUtil.getString(this, SharedPreUtil.KEY_STUDENT_ID, "0");

        BmobQuery<Student> query = new BmobQuery<>();
        query.addWhereEqualTo(Const.FIELD_STUDENT_ID, Integer.parseInt(studentId));
        query.setLimit(10);
        query.findObjects(new FindListener<Student>() {

            @Override
            public void done(List<Student> object, BmobException e) {
                if (e == null) {
                    adapter.setData(object);
                } else {
                    ToastUtil.showToast("请求数据失败！" + e.getMessage());
                }
            }

        });
    }

    @OnClick(R.id.fab)
    public void onViewClicked() {
        Intent intent = new Intent(OutSchoolActivity.this, DataActivity.class);
        startActivityForResult(intent, Const.DATA_ACTIVITY_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Const.DATA_ACTIVITY_CODE) {
            if (resultCode == RESULT_OK) {
                getData();
                setResult(RESULT_OK);
            }
        }
    }
}
